package com.ebt.m.msgnote.presenter;

import android.content.Context;

import com.ebt.m.AppInitService;
import com.ebt.m.commons.buscomponent.listview.BaseListContract;
import com.ebt.m.commons.buscomponent.listview.BaseListPresenter;
import com.ebt.m.commons.buscomponent.listview.SectionItem;
import com.ebt.m.data.rxModel.api.MsgAPI;
import com.ebt.m.msgnote.MsgConstant;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author allen
 * Created by allen on 2018/7/27 11:09.
 */
public class MsgCenterListPresenter extends BaseListPresenter<BaseListContract.IView> {

    public MsgCenterListPresenter(Context context, BaseListContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void loadNew(Object... args) {
        refreshList();
    }

    private void refreshList() {
        iView.showProgress(true);
        MsgAPI opportunityApi = AppInitService.getMsgApi();
        opportunityApi.getMsgCenterList()
//                .filter(getFilterForFootviewStatus(true))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    iView.updateData(createSectionItem(response.getData()));
                }, throwable -> {
                    throwable.printStackTrace();
                    iView.errorData();
                });
    }

    @Override
    public void refresh() {
        refreshList();
    }

    @Override
    public void loadNext(Object... args) {

    }

    @Override
    public List<SectionItem> createSectionItem(List datas) {
        List<SectionItem> list = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            if (i == datas.size() - 1) {
                SectionItem si = new SectionItem(MsgConstant.MSG_CENTER_VIEW_TYPE, datas.get(i));
                si.extras = MsgConstant.HIDE_LINE;
                list.add(si);
            } else {
                list.add(new SectionItem(MsgConstant.MSG_CENTER_VIEW_TYPE, datas.get(i)));
            }
        }
        return list;
    }
}
